/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Homework6;

import Homework6.CustomEvents.ISaveLoadFileListener;
import Homework6.CustomEvents.SaveLoadFileEvent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author Jakim
 */
class MenuFile extends Menu{
    // Private fields
    private MenuItem _menuItemNew;
    private MenuItem _menuItemOpen;
    private MenuItem _menuItemSave;
    private MenuItem _menuItemExit;
    private FileDialog _fileDialogLoad = null;
    private File _fileToBeLoaded = null;
    private File _fileToBeSave = null;

    protected Vector listenerList = new Vector();

    // Construktor
    public MenuFile(){
        super("File");

        this._menuItemNew = new MenuItem("New");
        
        this._menuItemOpen = new MenuItem("Open");
        this._menuItemOpen.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String curFile;
                Frame f = (Frame) TextEditorMenu.MenuBar.getParent();
                _fileDialogLoad = new FileDialog(f, "Зареди Файл", FileDialog.LOAD);
                _fileDialogLoad.setPreferredSize(new Dimension(500, 300));
                _fileDialogLoad.setFile ("*.txt");
                _fileDialogLoad.setVisible(true);
                if ((curFile = _fileDialogLoad.getFile()) != null){
                    String filePath = _fileDialogLoad.getDirectory() + curFile;
                    _fileToBeLoaded = new File(filePath);
                    if(!_fileToBeLoaded.canRead()){
                        _fileToBeLoaded.setReadable(true);
                    }
                    fireLoadFileEvent(new SaveLoadFileEvent(_fileToBeLoaded));
                }
            }
        });

        this._menuItemSave = new MenuItem("Save");
        this._menuItemSave.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String curFile;
                Frame f = (Frame) TextEditorMenu.MenuBar.getParent();
                _fileDialogLoad = new FileDialog(f, "Запищи Файл", FileDialog.SAVE);
                _fileDialogLoad.setPreferredSize(new Dimension(500, 300));
                _fileDialogLoad.setFile ("*.txt");
                _fileDialogLoad.setVisible(true);
                if ((curFile = _fileDialogLoad.getFile()) != null){
                    String filePath = _fileDialogLoad.getDirectory() + curFile;
                    _fileToBeLoaded = new File(filePath);
                    if(!_fileToBeLoaded.canRead()){
                        _fileToBeLoaded.setReadable(true);
                    }
                    fireSaveFileEvent(new SaveLoadFileEvent(_fileToBeLoaded));
                }
            }
        });
        this._menuItemSave.setEnabled(false);

        this._menuItemExit = new MenuItem("Exit");
        this._menuItemExit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                TextRedactor tmpFrame = ((TextRedactor)((TextEditorMenu)((MenuFile)_menuItemExit.getParent()).getParent()).getParent());
                processEvent(new WindowEvent(tmpFrame, WindowEvent.WINDOW_CLOSING));
            }
        });
        
        this.add(this._menuItemNew);
        this.add(this._menuItemOpen);
        this.add(this._menuItemSave);
        this.addSeparator();
        this.add(this._menuItemExit);
    };

    // Getters    
    public MenuItem getMenuItemExit(){
        return _menuItemExit;
    }

    public MenuItem getMenuItemNew(){
        return _menuItemNew;
    }

    public MenuItem getMenuItemOpen(){
        return _menuItemOpen;
    }

    public MenuItem getMenuItemSave(){
        return _menuItemSave;
    }

    public File getFileToBeLoaded(){
        return this._fileToBeLoaded;
    }

    // Event Listeners
    public void addSaveFileListener(ISaveLoadFileListener listener){
        this.listenerList.add(listener);
    }

    public void addLoadFileListener(ISaveLoadFileListener listener){
        this.listenerList.add(listener);
    }
    
    public void removeSaveFileListener(ISaveLoadFileListener listener){
        this.listenerList.remove(listener);
    }
    public void removeLoadFileListener(ISaveLoadFileListener listener){
        this.listenerList.remove(listener);
    }

    void fireSaveFileEvent(SaveLoadFileEvent e){
        Iterator itr = this.listenerList.iterator();
        while(itr.hasNext()) {
            ((ISaveLoadFileListener)itr.next()).fileSaveEventOccured(e);
      }
    }

    void fireLoadFileEvent(SaveLoadFileEvent e){
        Iterator itr = this.listenerList.iterator();
        while(itr.hasNext()) {
            ((ISaveLoadFileListener)itr.next()).fileLoadEventOccured(e);
      }
    }

}
